import pygtk
import gtk
from gtk import DrawingArea as DA

class tela(DA):
    def __init__(self):
        DA.__init__(self)
        self.definecores()

        self.apertou=False
        self.connect('expose-event',self.expose)
        self.add_events(gtk.gdk.BUTTON_PRESS_MASK |
    		gtk.gdk.BUTTON_RELEASE_MASK |
    		gtk.gdk.MOTION_NOTIFY |
	    	gtk.gdk.POINTER_MOTION_MASK 
            )
        self.connect('button-press-event',self.clica)
        self.connect('button-release-event',self.solta)
        self.connect('motion-notify-event',self.arrasta)
        self.connect('scroll-event',self.rolou)
        self.linha=[]
        self.xantes=None
        self.yantes=None

    def definecores(self):
        self.verde=gtk.gdk.Color(0,48255,0,0)
        self.preto=gtk.gdk.Color(0,0,0,0)
        self.branco=gtk.gdk.Color(65535,65535,65535,0)   
        self.vermelho=gtk.gdk.Color(65535,0,0,0)   
        self.amarelinho=gtk.gdk.Color(35535,65333,15000,0)   
        self.outra=gtk.gdk.Color(0,65535,65535,0)   
        self.azul=gtk.gdk.Color(0,0,65535,0)   
        
    def rolou(self,tela,evento):
        try:
            self.linha.pop()
            self.desenha()
        except:
            pass

    def arrasta(self,tela,evento):
        if self.apertou:
            self.linha.append((evento.x,evento.y))
            self.desenhaultimo()    
    
    def desenhaultimo(self):
        if len(self.linha)>1:
            x1=self.linha[-2][0]
            y1=self.linha[-2][1]
            x2=self.linha[-1][0]
            y2=self.linha[-1][1]
            
            self.gc.set_rgb_fg_color(self.vermelho)    
            self.window.draw_line(self.gc,x1,y1,x2,y2)
            
            self.gc.set_rgb_fg_color(self.preto)    
            self.window.draw_rectangle(self.gc,False,x1-1,y1-1,2,2,)
            self.window.draw_rectangle(self.gc,False,x2-1,y2-1,2,2,)

    def solta(self, tela, evento):
        self.apertou=False
        self.desenha()

    def clica(self, tela, evento):
        if evento.button==3:
            self.linha.pop()
            return 
        self.linha.append((evento.x,evento.y))
        self.apertou=True

    def desenha(self):
        a,b,c,d = self.get_allocation()
        self.gc.set_rgb_fg_color(self.verde)    
        self.window.draw_rectangle(self.gc,True,0,0,c-1,d-1)
        xantes=self.linha[0][0]
        yantes=self.linha[0][1]
        
        
        self.gc.set_rgb_fg_color(self.amarelinho)    
    	self.window.draw_polygon(self.gc,True, self.linha) 
        self.gc.set_rgb_fg_color(self.azul)    
    	self.window.draw_polygon(self.gc,False, self.linha) 
        
        self.window.draw_rectangle(self.gc,False,xantes-1,yantes-1,2,2,)

        self.gc.set_rgb_fg_color(self.preto)    
        for pt in self.linha:
            self.window.draw_rectangle(self.gc, False,pt[0]-1, pt[1]-1,2,2)

    def expose(self,tela, evento):
        self.gc=self.get_style().fg_gc[gtk.STATE_NORMAL]
        self.gc.set_rgb_fg_color(self.branco)    
        self.desenha()



if __name__ == '__main__':
    win=gtk.Window()
    tel=tela()
    win.add(tel)
    win.show_all()
    win.connect('destroy',gtk.main_quit)
    gtk.main()

